//
//  ChooseThemeViewController.m
//  Lightthings
//
//  Created by Hero on 2024/8/30.
//

#import "ChooseThemeViewController.h"
#import "ChooseThemeCell.h"

@interface ChooseThemeViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) NSArray * dataArr;

@end

@implementation ChooseThemeViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)initData {
    self.dataArr = @[@"icon_theme_default", @"icon_theme_dark_blue", @"icon_theme_red", @"icon_theme_purple", @"icon_theme_green", @"icon_theme_black", @"icon_theme_light_blue"];
    
}

- (void)initUI {
    self.navigationItem.title = SWString(@"choose Theme");
    [self.view addSubview:self.collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ChooseThemeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChooseThemeCell" forIndexPath:indexPath];
    
    cell.imgView.image = U_imageNamed(self.dataArr[indexPath.row]);
    if (indexPath.row + 1== [ThemeManager sharedInstance].themeValue) {
        cell.pointView.backgroundColor = jy_HexColor(@"#F57A36");
    }else {
        cell.pointView.backgroundColor = jy_HexColor(@"#cccccc");
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%ld",indexPath.row + 1] forKey:@"themeValue"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"k_change_theme" object:nil];
    
    [self back];
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(kScreenWidth / 2 - 30 , 240);
        layout.minimumLineSpacing = 20;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 0, kScreenWidth - 40, kScreenHeight - kNavigationBarHeight -kHomeIndicatorHeight) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerNib:[UINib nibWithNibName:@"ChooseThemeCell" bundle:nil] forCellWithReuseIdentifier:@"ChooseThemeCell"];
    }
    return _collectionView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
